<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="issShow" :visible="showDialog" @close="handleCloseDialog" @open="getEmployeeSimple">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <el-form ref="fromRef" label-width="120px" :model="form" :rules="formRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%">
          <el-option v-for="item in peoples" :key="item.id" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimpl } from '@/api/employees' // 下拉列表数据
import { reqAddDepartments, reqPutDepartDetail } from '@/api/departments' // 添加修改部门数据
export default {
  name: 'AddDepts',
  props: {
    showDialog: {
      default: false,
      type: Boolean
    },
    deptsList: {
      required: true,
      type: Array
    },
    nodeData: {
      required: true,
      type: Object
    }
  },
  data() {
    // 校验部门名称
    const checkNameRepeat = (rule, value, callback) => {
      let arr = []
      // 编辑部门时才有id
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        }
        arr = this.deptsList.filter(item => item.pid === this.nodeData.pid)
      } else {
        arr = this.deptsList.filter(item => item.id === this.nodeData.id)
      }
      const required = arr.some(item => item.name === value)
      required ? callback(new Error('部门名称重复')) : callback()
    }
    // 校验部门编码
    const checkCodeRepeat = (rule, value, callback) => {
      // 编辑部门时才有id
      if (this.form.id && this.nodeData.code === value) {
        callback()
        return
      }
      const required = this.deptsList.some(item => item.code === value)
      required ? callback(new Error('部门编码已存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      },
      formRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      peoples: [] // 渲染下拉列表
    }
  },
  computed: {
    issShow() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    // X按钮 关闭清空form表单
    handleCloseDialog() {
      this.$emit('update:showDialog', false) // 关闭弹窗
      this.$refs.fromRef.resetFields() // 清空form表单
      this.form = { // 关闭时清空数据 易分辨有无id
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      }
    },
    // open回调函数来接收axios数据
    async  getEmployeeSimple() {
      const res = await reqGetEmployeeSimpl()
      this.peoples = res.data
    },
    // 点击新增部门//编辑部门
    clickSubmit() {
      this.$refs.fromRef.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          const res = await reqPutDepartDetail(this.form)
          this.$message.success(res.message)
        } else {
          const res = await reqAddDepartments({ ...this.form, pid: this.nodeData.id }) // 这里传的是id 不是pid
          this.$message.success(res.message)
        }
        this.$emit('add-success')
        this.handleCloseDialog() // 调用X按钮
      })
    }
  }
}
</script>
